<?php

namespace App\Http\Requests\Admin\V1\DevicePreset;

use App\Http\Requests\ApiRequest;
use App\Models\Device;
use App\Rules\ImageFile;

/**
 * @property array descriptions
 * @property string device_id
 */
class DevicePresetDataRequest extends ApiRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'device_id'           => 'required|int',
            'name'                => 'required|string',
            'luminance'           => 'required|int',
            'color_temperature'   => 'required|int',
            'preset'              => 'nullable|json',
            'sort_order'          => 'required|int',
            'is_show'             => 'required|int',
            'descriptions'        => 'required|array',
            'descriptions.*.name' => 'required|string',
            'product_img'         => [(new ImageFile)->limitSize(5 * 1024)],
        ] + $this->deviceSettingParamLimitRule();
    }

    private function deviceSettingParamLimitRule(): array
    {
        /** @var Device $device */
        $device = (new Device)->find($this->device_id);

        if ($device) {
            return [
                'luminance_min'                  => "required|int|min:$device->luminance_min",
                'luminance_max'                  => "required|int|max:$device->luminance_max",
                'color_temperature_min'          => "required|int|min:$device->color_temperature_min",
                'color_temperature_max'          => "required|int|max:$device->color_temperature_max",
            ];
        } else {
            return [];
        }
    }
}
